<?php
namespace app\api\controller\v1\controller\pet;

use app\api\controller\v1\model\pet\Appointment as Ap;
use think\Controller;
use app\api\controller\Api;
use think\Model;
use think\Db;
use think\Image;
use think\Request;
use JPush\Client as JPush;
use think\Response;
use app\api\controller\UnauthorizedException;
use think\Validate;


class Appointment extends api
{
//    判断宠粮是否足够约宠
    public function judgement_pet_food($user_id){
        $pet_food = Ap::judgementPetFood($user_id);

        if ($pet_food >=70) {
            return $this->returnmsg(200,'可以约宠');
        }else{
            return $this->returnmsg(401,'宠粮余额不足以约宠');
        }
    }




//约宠功能
    public function appointment_pet($user_id,$pet_id,$number_of_people,$appointment_location,$appointment_remarks,$appointment_time){
        $time = date('Y-m-d H:i:s');
        $owned = Db::table('ym_pet')->field('user_id')->where('pet_id',$pet_id)->select();
        $owned_user_id = $owned[0]['user_id'];
        $result = Db::table('user_info')->field('user_icon')->where('user_id',$user_id)->select();
        $user_icon = $result[0]['user_icon'];
        if (!empty($appointment_remarks)) {
            $data = ['user_id'=>$user_id,'pet_id'=>$pet_id,'owned_user_id'=>$owned_user_id,'user_icon'=>$user_icon,'number_of_people'=>$number_of_people,'appointment_location'=>$appointment_location,'appointment_remarks'=>$appointment_remarks,'appointment_time'=>$appointment_time];

            $appointment_id = Db::table('ym_pet_appointment')->insertGetId($data);

        }else{
            $data = ['user_id'=>$user_id,'pet_id'=>$pet_id,'owned_user_id'=>$owned_user_id,'user_icon'=>$user_icon,'number_of_people'=>$number_of_people,'appointment_location'=>$appointment_location,'appointment_time'=>$appointment_time];
            $appointment_id = Db::table('ym_pet_appointment')->insertGetId($data);
        }
        $message_data = ['be_user_id'=>$owned_user_id,'user_id'=>$user_id,'message_type'=>1,'create_time'=>$time];
        $m_id = Db::table('ym_message')->insertGetId($message_data);
        $appointment_data = ['m_id'=>$m_id,'appointment_id'=>$appointment_id];
        $m_appointment_id = Db::table('ym_message_appointment')->insertGetId($appointment_data);

        $user_name = Db::query("select user_name from user_info where user_id = '$user_id'");
        $name = $user_name[0]['user_name'];
        $pet = Db::query("select pet_name from ym_pet where pet_id = '$pet_id'");
        $pet_name = $pet[0]['pet_name'];

        $owned_user_id = strval($owned_user_id);
        $this->push($owned_user_id,$name.'约了你的宠物:'.$pet_name);
        return $this->returnmsg(200,'成功');
    }



//接受或拒绝约宠及理由
    public function agree_appointment_pet($appointment_agree,$reason,$user_id,$appointment_id){

        $time = date('Y-m-d H:i:s');
        $be_id = Db::query("select user_id from ym_pet_appointment where appointment_id = '$appointment_id'");
        $be_user_id = $be_id[0]['user_id'];//接受消息方，约宠方
        $user_name = Db::query("select user_name from user_info  where user_id = '$user_id'");
        $name = $user_name[0]['user_name'];
        if ($appointment_agree == 0) {//拒绝收养
            $refuse_data = ['appointment_id'=>$appointment_id,'user_id'=>$user_id,'appointment_agree'=>$appointment_agree,'refuse_reason'=>$reason];
            Db::table('ym_pet_appointment_agree_or_not')->insert($refuse_data);

            Db::query("update ym_pet_appointment set appointment_status = 0 where appointment_id = '$appointment_id'");

            //message
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>8,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $appointment_data = ['m_id'=>$m_id,'refuse_reason'=>$reason,'appointment_id'=>$appointment_id];
            $m_appointment_id = Db::table('ym_message_appointment_agree_or_not')->insertGetId($appointment_data);

            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'拒绝了你的约宠,原因:'.$reason);
            return $this->returnmsg(200,"拒绝收养",$refuse_data);
        }else{
            $agree_data = ['appointment_id'=>$appointment_id,'user_id'=>$user_id,'appointment_agree'=>$appointment_agree];
            Db::table('ym_pet_appointment_agree_or_not')->insert($agree_data);

            Db::query("update ym_pet_appointment set appointment_status = 1 where appointment_id = '$appointment_id'");

            //message
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>8,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $appointment_data = ['m_id'=>$m_id,'appointment_id'=>$appointment_id];
            $m_appointment_id = Db::table('ym_message_appointment_agree_or_not')->insertGetId($appointment_data);

            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'同意收养的你宠物');
            return $this->returnmsg(200,"同意收养",$agree_data);
        }
    }


//    显示约宠信息
    public function show_appointment($appointment_id){
        $result = Ap::getShowAppointment($appointment_id);
        return $this->returnmsg(200,"寄养表信息",$result);
    }

    /**
     * 获取现在约宠状态
     * @param $user_id  用户id
     */
    public function get_appointment_status($user_id)
    {
        $result = Ap::getAppointmentInfo($user_id);
        if($result == 'error')
        {
            return $this->returnmsg(401,"数据库查询崩溃");
        }
        if(empty($result))
        {
            return $this->returnmsg(403,"查找不到用户");
        }
        $data = $result['appointment_or_not'];
        return $this->returnmsg(200,"操作成功",$data);
    }

    //是否打开约溜开关
    public function appointment_or_not($switch)
    {
        $user_id = $_POST['user_id']?$_POST['user_id']:$_GET['user_id'];
        $data = [
            'user_id' => $user_id,
            'switch' => $switch,
        ];
        $result = Ap::setAppointmentSwitch($data);
        if($result == 200 )
        {
            return $this->returnmsg(200,"操作成功");
        }else if($result == 201){
            return $this->returnmsg(301,"操作失败，传入参数不合法");
        }else if($result == 202){
            return $this->returnmsg(302,"操作失败，数据库查询出错");
        }else
        {

        }

    }




    //推送全部
    public function push($user_id,$message){
        $appkey = 'a3d1bad973276db7cee3f40a';
        $master_secret = 'ac5c6c6b1101643082c88913';
        $client = new JPush($appkey,$master_secret);
        $r_id = Db::query("select registration_id from ym_device_no where user_id = '$user_id'");
        $registration_id = $r_id[0]['registration_id'];

        $push = $client->push();
        $push->setPlatform('all');
        //别名
        $push->addAlias($user_id);
        //设备号
        $push->addRegistrationId($registration_id);
        // $push->iosNotification();
        $push->iosNotification($message, [
            'sound' => 'sound.cat',
            'badge' => '+1',

        ]);
        $push->options(array(
            "apns_production" => true
        ));
        // try {
        $push->send();

    }



}